﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using Deanery.Controller.Directories;
using Deanery.Controller.PersonData;

namespace Deanery.Controller.Service
{
    internal class PersonController
    {
        private readonly deaneryEntities _db = new deaneryEntities();
        

        private Guid Region(string region)
        {
            var reccord = new ArrayList {region};

            var obj = new RegionController();
            if (obj.FindGuidByName(region) == default(Guid))
                obj.Insert(reccord);

            return obj.FindGuidByName(region);
        }

        private Guid TypeCity(string typeCity)
        {
            var reccord = new ArrayList {typeCity};

            var obj = new TypeCityController();
            if (obj.FindGuidByName(typeCity) == default(Guid))
                obj.Insert(reccord);

            return obj.FindGuidByName(typeCity);
        }

        private Guid City(string region, string city)
        {
            var obj = new CityController();
            if (obj.FindGuidByCityName(region, city) == default(Guid))
                obj.Insert(region, city);

            return obj.FindGuidByCityName(region, city);
        }

        private Guid TypeStreet(string typeStreet)
        {
            var reccord = new ArrayList { typeStreet };

            var obj = new TypeStreetController();
            if (obj.FindGuidByName(typeStreet) == default(Guid))
                obj.Insert(reccord);

            return obj.FindGuidByName(typeStreet);
        }

        private Guid Street(string street)
        {
            var obj = new StreetController();
            if (obj.FindGuidByStreetName(street) == default(Guid))
                obj.Insert(street);

            return obj.FindGuidByStreetName(street);
        }

        private Guid Address(Guid city, Guid street, string house, string apartment)
        {
            var obj = new AddressController();
            if (obj.FindAddress(city, street, house, apartment) == default(Guid))
                obj.Insert(city, street, house, apartment);

            return obj.FindAddress(city, street, house, apartment);
        }

        private Guid TypeContact(string typeContact)
        {
            var reccord = new ArrayList { typeContact };

            var obj = new TypeContactsController();
            if (obj.FindGuidByName(typeContact) == default(Guid))
                obj.Insert(reccord);
            return obj.FindGuidByName(typeContact);
        }

        private Guid Contact(Guid type, string contact, Guid person)
        {
            var obj = new ContactsController();
            if (obj.FindGuidByContactDescription(type, contact) == default(Guid))
                obj.Insert(type, contact, person);
            return obj.FindGuidByContactDescription(type, contact);
        }

        private Guid Sex(string sex)
        {
            var obj = new SexController();
            return obj.FindGuidByName(sex);
        }

        private Guid TypeDocument(string typeDoc)
        {
            var reccord = new ArrayList { typeDoc };

            var obj = new TypeDocumentsController();
            if (obj.FindGuidByName(typeDoc) == default(Guid))
                obj.Insert(reccord);
            return obj.FindGuidByName(typeDoc);
        }

        private Guid Document(Guid type, string series, string number, string data, string issued, Guid person)
        {
            var obj = new DocumentController();
            if (obj.FindGuidByKeyFields(type, series, number, data, issued) == default(Guid))
                obj.Insert(type, series, number, data, issued, person);
            return obj.FindGuidByKeyFields(type, series, number, data, issued);
        }


        //public bool SavePerson(string firstName, string lastName, string middleName, string dateOfBirth, string sex,
        //                       string region, string city, string street, string numBuild, string numAppartment,
        //                       string seriesPass, string numPass, string datePass, string fromPass, string typeDoc,
        //                       string seriesDoc, string numDoc, string dateDoc, string fromDoc, string mark,
        //                       string award, string mobileNum, string typePay, string dateEdu, string remark)
        public bool SavePerson(DataRow item)
        {
            //region
            Region(item[6].ToString());
            //type city
            TypeCity(item[7].ToString());
            //city
            City(item[6].ToString(), item[7].ToString());
            //type street
            TypeStreet(item[8].ToString());
            //street
            Street(item[8].ToString());
            //address

            //type contacts
            //contacts
            //sex
            //type doc
            //doc
            /*
            var person = new Person
                {
                    id_person = Guid.NewGuid(),
                    nv_last_name = last_name,
                    nv_first_name = first_name,
                    nv_middle_name = middle_name,
                    dt_date_of_birth = date_of_birth,
                    id_address = address,
                    id_orders = orders,
                    id_status = status,
                    bl_hostel = hostel,
                    id_sex = sex,
                    guid_code_edbo = code_edbo,
                    int_edbo = edbo
                };

            _db.People.AddObject(person);
            _db.SaveChanges();
            */

            //type order
            //order
            //type privilege
            //privilege
            //privilee group
            //status
            //perents
            
            return true;
        }
    }
}
